/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.N450;
import queenb.net.entitybean.N850;
import queenb.web.crm.store.N850Store;
import queenb.web.store.N450Store;
import queenb.web.util.ComboboxUtil;
import queenb.web.util.SessionProvider;

/**
 *
 * @author Nguyen Hung
 */
public class N450_N850Controller extends QueenbController {

    private int fn450;
    private int fn850;
    private int group = -1;
    private String nv851;
    private String nv451;
    private int fh200;
    private String hv201;
    private String loginEmp;
    private ComboboxUtil comUtil;
    private List<SelectItem> listComb850 = new ArrayList<SelectItem>();

    public N450_N850Controller() {
        try {
            if (getLogin() != null) {
                //fn450 = getLogin().getFn450();
                //  fn850 = getLogin().getFn850();
                fn450 = 0;
                fn850 = getLogin().getFn850();
                nv851 = getLogin().getNv851();
                nv451 = getLogin().getNv451();
                fh200 = getLogin().getCompanyNo();
                hv201 = getLogin().getCompanyName();
                loginEmp = getLogin().getUserName();

            } else {
                removeSession();
                sendRedirect("login.jsp");
            }
            comUtil = new ComboboxUtil();
//            Object objH200 = SessionProvider.getSessionMapValue("companyID");
//            Object objN450 = SessionProvider.getSessionMapValue("depID");
            Object objN850 = SessionProvider.getSessionMapValue("groupID");
            Object objReN850 = getRequest().getParameter("fn850");
            
           
            //fn850 lay tu request
            if (objReN850 != null) {
                fn850 = Integer.parseInt(objReN850.toString());
                System.out.println("-------requestN850----" + fn850);                
            } else if (objN850 != null) { // session
                fn850 = Integer.parseInt(objN850.toString());
                System.out.println("-------session N850---" + fn850);                
            }
            getListComb850();
            Object objEmp = SessionProvider.getSessionMapValue("emp");
            Object objReEmp = getRequest().getParameter("emp");
            if(objReEmp !=null){
                loginEmp = objReEmp.toString();
            }else if(objEmp !=null){
                loginEmp = objEmp.toString();
            }
//            SessionProvider.setSessionMapValue("companyID", fh200);
            SessionProvider.setSessionMapValue("emp", loginEmp);
            SessionProvider.setSessionMapValue("groupID", fn850);

            System.out.println("--------fh200--:" + fh200 + "----fn450--:" + fn450 + "----fn850--:" + fn850+"---emp--"+loginEmp);
        } catch (Exception e) {
        }
    }

    public List<N450> getListN450Comb() {
        try {
            N450Store n450Store = new N450Store();
            List<N450> listCombN450 = n450Store.findAllN450ForCombo(getLogin().getModule(), fh200, getLogin().getUserName());

            for (int i = 0; i < listCombN450.size(); i++) {
                N450 n450 = listCombN450.get(i);

                if (n450.getVal() == getFn450()) {
                    setNv451(n450.getLabel());
                    SessionProvider.setSessionMapValue("depName", nv451);


                    break;
                }
            }
            System.out.println("----------depID-----" + fn450);
            System.out.println("-------getListN450Comb-------" + listCombN450.size());
            return listCombN450;
        } catch (Exception e) {
            return null;
        }

    }

//    public List<N450> getListH200Comb() {
//        try {
//            N450Store n450Store = new N450Store();
//            List<N450> listCombN450 = n450Store.findAllH200ForCombo(getLogin().getModule(), getLogin().getUserName());
//
//            for (int i = 0; i < listCombN450.size(); i++) {
//                N450 n450 = listCombN450.get(i);
//                if (n450.getVal() == fh200) {
//                    setHv201(n450.getLabel());
//                    SessionProvider.setSessionMapValue("companyName", hv201);
//
//                    break;
//                }
//            }
//            System.out.println("-----------companyID----" + fh200);
//            System.out.println("-------getListH200Comb-------" + listCombN450.size());
//            return listCombN450;
//        } catch (Exception e) {
//            return null;
//        }
//
//    }
    /**
     * @return the fn450
     */
    public int getFn450() {
        return fn450;
    }

    /**
     * @param fn450 the fn450 to set
     */
    public void setFn450(int fn450) {
        this.fn450 = fn450;
    }

    /**
     * @return the fn850
     */
    public int getFn850() {
        return fn850;
    }

    /**
     * @param fn850 the fn850 to set
     */
    public void setFn850(int fn850) {
        this.fn850 = fn850;
    }

    /**
     * @return the group
     */
    public int getGroup() {
        return group;
    }

    /**
     * @param group the group to set
     */
    public void setGroup(int group) {
        this.group = group;
    }

    /**
     * @return the nv851
     */
    public String getNv851() {
        return nv851;
    }

    /**
     * @param nv851 the nv851 to set
     */
    public void setNv851(String nv851) {
        this.nv851 = nv851;
    }

    /**
     * @return the nv451
     */
    public String getNv451() {
        return nv451;
    }

    /**
     * @param nv451 the nv451 to set
     */
    public void setNv451(String nv451) {
        this.nv451 = nv451;
    }

    /**
     * @return the fh200
     */
    public int getFh200() {
        return fh200;
    }

    /**
     * @param fh200 the fh200 to set
     */
    public void setFh200(int fh200) {
        this.fh200 = fh200;
    }

    /**
     * @return the hv201
     */
    public String getHv201() {
        return hv201;
    }

    /**
     * @param hv201 the hv201 to set
     */
    public void setHv201(String hv201) {
        this.hv201 = hv201;
    }

    /**
     * @return the loginEmp
     */
    public String getLoginEmp() {
        return loginEmp;
    }

    /**
     * @param loginEmp the loginEmp to set
     */
    public void setLoginEmp(String loginEmp) {
        this.loginEmp = loginEmp;
    }

    /**
     * @return the listComb850
     */
    public List<SelectItem> getListComb850() {
        try {
//            List<N850> listComb850 = n850Store.findAllN850ForCombo(getLogin().getModule(), fh200, getFn450(), getLogin().getUserName());
            if (listComb850.size() < 1 || listComb850 == null) {
                listComb850 = comUtil.getSelectedN850Comb();

                for (int i = 0; i < listComb850.size(); i++) {
                    if (Integer.parseInt(listComb850.get(i).getValue().toString()) == fn850) {
                        setNv851(listComb850.get(i).getLabel());
                        break;
                    }

                }
                System.out.println("-----------groupID----" + fn850);
                System.out.println("-------getListN850Comb-------" + listComb850.size());

            }
        } catch (Exception e) {
            listComb850 = new ArrayList<SelectItem>();
        }
        return listComb850;
    }

    /**
     * @param listComb850 the listComb850 to set
     */
    public void setListComb850(List<SelectItem> listComb850) {
        this.listComb850 = listComb850;
    }
}
